﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Web;
using System.Web.SessionState;

namespace LCrs.Web.kuinfo
{
    /// <summary>
    /// GetKu 的摘要说明
    /// </summary>
    public class GetKu : IHttpHandler, IRequiresSessionState
    {

        StringBuilder result = new StringBuilder();
        StringBuilder sb = new StringBuilder();
        StringBuilder rechild = new StringBuilder();
        LCrs.BLL.kuinfo bll = new BLL.kuinfo();
        public void ProcessRequest(HttpContext context)
        {
            string ac = "";
            if (null != context.Request.Form["action"])
            {//获取前台传来的值  
                ac = context.Request.Form["action"].ToString().Trim();
            }
            else if (null != context.Request.QueryString["action"])
            {
                ac = context.Request.QueryString["action"].ToString().Trim();
            }

            if (ac == "query")
                Query(context);//全 国数据
            else if (ac == "add")
                add(context);
            else if (ac == "modify")
                modify(context);
            else if (ac == "delete")
                delete(context);
            else if (ac == "addmyuid")
                addmyuid(context);
            else if (ac == "deletemyuid")
                deletemyuid(context);
            else if (ac == "querymyuid")
                Querymyuid(context);//uid所在省内数据
            else if (ac == "queryyb")
                Queryyb(context);
            else if (ac == "queryybk")
                Queryybk(context);


        }

        private string GetTreeJsonByTablea(DataTable tabel, string idCol, string txtCol, string addattr)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol].ToString().Trim() + "\",\"type\":\"" + addattr + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void deletemyuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pid = 0;
                string hids = context.Request.QueryString["pid"];
                if (hids != "") pid = int.Parse(hids);
                if (pid != 0)
                {
                    if (bll.Delete(pid))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();
                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();

                    }
                }
            }

        }

        private string GetTreechild(DataTable tabel, string idCol, string rela, object pId)
        {
            rechild.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append(row[idCol] + ",");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            GetTreechild(tabel, idCol, rela, row[idCol]);
                            rechild.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        rechild.Append(sb.ToString());
                        sb.Remove(0, sb.Length);
                    }
                    //sb = sb.Remove(sb.Length - 1, 1);
                }
                rechild.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return rechild.ToString();
        }
        public void delete(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            int uid = 0;
            string uidstr = context.Request.QueryString["kid"];
            if (uidstr != "")
                uid = int.Parse(uidstr);
            if (bll.Delete(uid))
            {
                context.Response.Write("T");//返回给前台页面  
                context.Response.End();
            }
            else
            {
                context.Response.Write("F");//返回给前台页面  
                context.Response.End();

            }

        }
        public void modify(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int hid = int.Parse(context.Request.Form["kid"]);
                string kname = context.Request.Form["kname"];

                LCrs.Model.kuinfo model = new LCrs.Model.kuinfo();
                model.kname = kname;
                model.kid = hid;
                if (bll.Update(model))
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void add(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                string hname = context.Request.Form["kname"];
                int parentid = 0;
                if (context.Request.Form["parentid"] != "")
                    parentid = int.Parse(context.Request.Form["parentid"]);
                LCrs.Model.kuinfo model = new LCrs.Model.kuinfo();
                model.kname = hname;
                model.parentid = parentid;
                if (bll.Add(model) > 0)
                {
                    context.Response.Write("T");//返回给前台页面  
                    context.Response.End();

                }
                else
                {
                    context.Response.Write("F");//返回给前台页面  
                    context.Response.End();
                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }

        public void addmyuid(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                int pid = 0;
                string hids = context.Request.QueryString["pid"];
                if (hids != "") pid = int.Parse(hids);
                if (pid != 0)
                {
                    string kuids = context.Request.Form["kuid"];
                    if (bll.Update(kuids,pid))
                    {
                        context.Response.Write("T");//返回给前台页面  
                        context.Response.End();

                    }
                    else
                    {
                        context.Response.Write("F");//返回给前台页面  
                        context.Response.End();
                    }

                }
            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }

        }
        private string GetTreeJsonByTableaa(DataTable tabel, string idCol, string txtCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        if (row[idCol].ToString() == "140")
                        {
                            string a = "";
                        }
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"type\":\"2\",\"bm\":\"" + row["abh"] + "\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            //点击展开
                            sb.Append(",\"children\":");
                            GetTreeJsonByTableaa(tabel, idCol, txtCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length); ;
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        private string GetTreeJsonByTable(DataTable tabel, string idCol, string txtCol, string rela, object pId)
        {
            result.Append(sb.ToString());
            sb.Remove(0, sb.Length);
            if (tabel.Rows.Count > 0)
            {
                sb.Append("[");
                string filer = string.Format("{0}='{1}'", rela, pId);
                DataRow[] rows = tabel.Select(filer);
                if (rows.Length > 0)
                {
                    foreach (DataRow row in rows)
                    {
                        sb.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\"");
                        if (tabel.Select(string.Format("{0}='{1}'", rela, row[idCol])).Length > 0)
                        {
                            //点击展开
                            sb.Append(",\"children\":");
                            GetTreeJsonByTable(tabel, idCol, txtCol, rela, row[idCol]);
                            result.Append(sb.ToString());
                            sb.Remove(0, sb.Length);
                        }
                        result.Append(sb.ToString());
                        sb.Remove(0, sb.Length); ;
                        sb.Append("},");
                    }
                    sb = sb.Remove(sb.Length - 1, 1);
                }
                sb.Append("]");
                result.Append(sb.ToString());
                sb.Remove(0, sb.Length);
            }
            return result.ToString();
        }
        private string GetTreeJsonByTableOne(DataTable tabel, string idCol, string txtCol, string bmCol)
        {
            result.Remove(0, result.Length);
            if (tabel.Rows.Count > 0)
            {
                result.Append("[");

                foreach (DataRow row in tabel.Rows)
                {
                    result.Append("{\"id\":\"" + row[idCol] + "\",\"text\":\"" + row[txtCol] + "\",\"bm\":\"" + row[bmCol] + "\"},");
                }
                result = result.Remove(result.Length - 1, 1);

                result.Append("]");

            }
            return result.ToString();
        }
        public void Query(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            if (Web.Admin.myinfo.isLogin(context))
            {
                DataSet ds = bll.GetList(" 1=1 order by kname");
                string str = GetTreeJsonByTable(ds.Tables[0], "kid", "kname", "parentid", 0);
                context.Response.Write(str);//返回给前台页面  
                context.Response.End();
            }
        }
        public void Querymyuid(HttpContext context)
        {
            //context.Response.ContentType = "text/plain";
            //if (Web.Admin.myinfo.isLogin(context))
            //{
            //    string uids = context.Request.Params["uid"];
            //    int uid = 0;
            //    int.TryParse(uids, out uid);
            //    cebao.BLL.myharmful mybll = new BLL.myharmful();
            //    //string pro = Web.Admin.myinfo.getPCC(context).Split(',')[0];
            //    DataSet ds = mybll.GetListvuid(" uid=" + uid);
            //    string str = GetTreeJsonByTableOne(ds.Tables[0], "uhid", "hname", "hbm");
            //    context.Response.Write(str);//返回给前台页面  
            //    context.Response.End();
            //}
        }
        public void Queryybk(HttpContext context)//异步没有被占用的
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                int parentId = -1;
                if (!string.IsNullOrEmpty(context.Request.QueryString["pid"]))
                {
                    Int32.TryParse(context.Request.QueryString["pid"], out parentId);
                }
                if (parentId >= 0)
                {
                    List<LCrs.Model.kuinfo> areas = bll.GetModelList("pid is null and parentid=" + parentId);
                    string resultStr = "";
                    resultStr += "[";
                    foreach (LCrs.Model.kuinfo item in areas)
                    {
                        int i = bll.GetRecordCount("pid is null and parentid=" + item.kid);
                        resultStr += "{";
                        if (i > 0)
                            resultStr += string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\"", item.kid.ToString(), item.kname);
                        else
                            resultStr += string.Format("\"id\": \"{0}\", \"text\": \"{1}\"", item.kid.ToString(), item.kname);
                        resultStr += "},";
                    }
                    resultStr = resultStr.Substring(0, resultStr.Length - 1);
                    resultStr += "]";
                    context.Response.Write(resultStr);//返回给前台页面  
                    context.Response.End();
                }

            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public void Queryyb(HttpContext context)//异步
        {
            context.Response.ContentType = "text/plain";
            int uid = Web.Admin.myinfo.getID(context);
            if (uid != 0)
            {

                int parentId = -1;
                if (!string.IsNullOrEmpty(context.Request.QueryString["pid"]))
                {
                    Int32.TryParse(context.Request.QueryString["pid"], out parentId);
                }
                if (parentId >= 0)
                {
                    List<LCrs.Model.kuinfo> areas = bll.GetModelList("parentid=" + parentId);
                    string resultStr = "";
                    resultStr += "[";
                    foreach (LCrs.Model.kuinfo item in areas)
                    {
                        int i = bll.GetRecordCount("parentid=" + item.kid);
                        resultStr += "{";
                        if (i > 0)
                            resultStr += string.Format("\"id\": \"{0}\", \"text\": \"{1}\", \"state\": \"closed\"", item.kid.ToString(), item.kname);
                        else
                            resultStr += string.Format("\"id\": \"{0}\", \"text\": \"{1}\"", item.kid.ToString(), item.kname);
                        resultStr += "},";
                    }
                    resultStr = resultStr.Substring(0, resultStr.Length - 1);
                    resultStr += "]";
                    context.Response.Write(resultStr);//返回给前台页面  
                    context.Response.End();
                }

            }
            else
            {
                context.Response.Write("L");//返回给前台页面  
                context.Response.End();
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}